using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace Xioa_UI.Controls;

public class XioaWindow : Window
{
    private Border? _TitleGrid;
    private Button? _CloseButton;
    private Button? _MinSizeButton;
    private Button? _MaxSizeButton;

    public XioaWindow()
    {
        this.ContentRendered += XioaWindow_ContentRendered;
    }

    private void XioaWindow_ContentRendered(object? sender, EventArgs e)
    {
        _TitleGrid = this.GetTemplateChild("XioaTitle_Border") as Border;
        _TitleGrid.PreviewMouseDown += _TitleGrid_MouseDown;

        _MinSizeButton = this.GetTemplateChild("Xioa_MinSize_Button") as Button;
        _MinSizeButton.PreviewMouseDown += _MinSizeButton_Click;

        _MaxSizeButton = this.GetTemplateChild("Xioa_MaxSize_Button") as Button;
        _MaxSizeButton.PreviewMouseDown += _MaxSizeButton_Click;

        _CloseButton = this.GetTemplateChild("Xioa_Close_Button") as Button;
        _CloseButton.PreviewMouseDown += _CloseButton_PreviewMouseDown;
    }
    
    private void _MaxSizeButton_Click(object sender, MouseButtonEventArgs e)
    {
        if (this.WindowState== WindowState.Maximized)
        {
            this.WindowState = WindowState.Normal;
        }
        else
        {
            this.WindowState = WindowState.Maximized;
        }    
    }

    private void _CloseButton_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        base.Close();
    }

    private void _MinSizeButton_Click(object sender, RoutedEventArgs e)
    {
        this.WindowState = WindowState.Minimized;
    }

    private void _TitleGrid_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (Mouse.LeftButton == MouseButtonState.Pressed)
            DragMove();
    }

    public static readonly DependencyProperty MinSizeVisibilityProperty =
        ElementBase.Property<XioaWindow, Visibility>("MinSizeVisibilityProperty", Visibility.Visible);

    public static readonly DependencyProperty ItemIconProperty =
       ElementBase.Property<XioaWindow, BitmapImage>("ItemIconProperty", new BitmapImage(new Uri("../Assets/src/logo.png", UriKind.Relative)));
    public BitmapImage? ItemIcon
    {
        get => (BitmapImage)GetValue(ItemIconProperty);
        set => SetValue(ItemIconProperty, value);
    }



    public Visibility MinSizeVisibility
    {
        get => (Visibility)GetValue(MinSizeVisibilityProperty);
        set => SetValue(MinSizeVisibilityProperty, value);
    }

    public static readonly DependencyProperty WindowCornerRadiusProperty =
      ElementBase.Property<XioaWindow, CornerRadius>("WindowCornerRadiusProperty", new CornerRadius(8));
    public CornerRadius WindowCornerRadius
    {
        get => (CornerRadius)GetValue(WindowCornerRadiusProperty);
        set => SetValue(WindowCornerRadiusProperty, value);
    }



    public static readonly DependencyProperty WindowHeaderCornerRadiusProperty =
     ElementBase.Property<XioaWindow, CornerRadius>("WindowHeaderCornerRadiusProperty", new CornerRadius(8,8,0,0));
    public CornerRadius WindowHeaderCornerRadius
    {
        get => (CornerRadius)GetValue(WindowHeaderCornerRadiusProperty);
        set => SetValue(WindowHeaderCornerRadiusProperty, value);
    }

}